import requests
import json
from spiderx import settings
from spiderx.tools.client import redisClientx

lua_script = '''
assert(splash:go(args.url))
assert(splash:wait(10.0))
return splash:get_cookies()
'''
splash_url = settings.SCRAPYDAPI + '/run?url=https://s.weibo.com'
headers = {'content-type': 'application/json'}
data = json.dumps({'lua_source': lua_script})


class CkTask:
    def run(self):
        try:
            response = requests.post(splash_url, headers=headers, data=data)
            ck = response.json()
            cookie = ''
            for c in ck:
                if c['domain'] == '.weibo.com':
                    cookie = cookie + c['name'] + '=' + c['value'] + '; '

            redisClientx.getClient().set('WEIBO_COOKIES', cookie)
        except Exception as e:
            pass


ckTask = CkTask()